<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">deferred.promise()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/deferred-object/">延迟对象</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/deferred.promise/" target="_blank">deferred.promise()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="deferred-promise1"><h2 class="section-title">
<span class="name">deferred.promise(  [target ] )</span><span class="returns">返回: <a href="/Types/#Promise">Promise</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>返回Deferred(延迟)的Promise（承诺）对象。 </p>
<ul class="signatures"><li class="signature" id="deferred-promise-target">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.5/">1.5</a></span>deferred.promise(  [target ] )</h4>
<ul><li>
<div><strong>target</strong></div>
<div>类型: <a href="/Types/#Object">Object</a>
</div>
<div>绑定 promise 方法的对象。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>deferred.promise()</code> 
			    	方法允许一个异步函数阻止那些干涉其内部请求的进度（progress）或状态（status）的其它代码。Promise （承诺）对象仅会暴露那些需要绑定额外的处理或判断状态的延迟方法(<code>then</code>, <code>done</code>, <code>fail</code>, <code>always</code>, <code>pipe</code>, <code>progress</code>, 和 <code>state</code>)时，并不会暴露任何用于改变状态的延迟方法(<code>resolve</code>, <code>reject</code>, <code>notify</code>, <code>resolveWith</code>, <code>rejectWith</code>, 和 <code>notifyWith</code>).</p>
<p>如果提供<code>target</code>参数，<code>deferred.promise()</code> 会将事件绑定到该参数上，然后返回该对象，而不是创建一个新的对象。
			这个方法可以用于在已经存在的对象上绑定 Promise 行为的情况。</p>
<p>
			如果您要创建一个Deferred(延迟)，并且保持这个Deferred(延迟)的引用，以便它可以在一些点来解决或拒绝。通过<code>deferred.promise()</code>定义Promise（承诺）对象即可。这样的话，其它的代码就可以注册回调函数或检查当前状态。</p>
<p>欲了解更多信息，请参阅文档&lt; <a href="/category/deferred-object/">Deferred object</a>.</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4>Example: <span class="desc">创建一个延迟对象，并设定两个延时时间是随机的定时器，分别用于受理（resolve）和拒绝（reject）延迟对象。无论哪一个先执行，都会调用其中一个回调函数。而另一个定时器则不会产生任何效果，因为在最先调用的那个定时器处理中，延迟对象已经处于完成状态(resolved 或 rejected 状态)。同时，还会设定一个定时器进度（progress）通知函数，用于进度通知处理，并在文档的 "body" 中显示 "working..."。</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
<div class="line n37">37</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="function"><span class="keyword">function</span> <span class="title">asyncEvent</span><span class="params">()</span>{</span></code></div></div><div class="container"><div class="line"><code>    <span class="keyword">var</span> dfd = <span class="keyword">new</span> jQuery.Deferred();</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>    <span class="comment">// Resolve after a random interval</span></code></div></div><div class="container"><div class="line"><code>    setTimeout(<span class="keyword">function</span>(){</code></div></div><div class="container"><div class="line"><code>        dfd.resolve(<span class="string">"hurray"</span>);</code></div></div><div class="container"><div class="line"><code>    }, Math.floor(<span class="number">400</span>+Math.random()*<span class="number">2000</span>));</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>    <span class="comment">// Reject after a random interval</span></code></div></div><div class="container"><div class="line"><code>    setTimeout(<span class="keyword">function</span>(){</code></div></div><div class="container"><div class="line"><code>        dfd.reject(<span class="string">"sorry"</span>);</code></div></div><div class="container"><div class="line"><code>    }, Math.floor(<span class="number">400</span>+Math.random()*<span class="number">2000</span>));</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>    <span class="comment">// Show a "working..." message every half-second</span></code></div></div><div class="container"><div class="line"><code>    setTimeout(<span class="function"><span class="keyword">function</span> <span class="title">working</span><span class="params">()</span>{</span></code></div></div><div class="container"><div class="line"><code>        <span class="keyword">if</span> ( dfd.state() === <span class="string">"pending"</span> ) {</code></div></div><div class="container"><div class="line"><code>            dfd.notify(<span class="string">"working... "</span>);</code></div></div><div class="container"><div class="line"><code>            setTimeout(working, <span class="number">500</span>);</code></div></div><div class="container"><div class="line"><code>        }</code></div></div><div class="container"><div class="line"><code>    }, <span class="number">1</span>);</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>    <span class="comment">// Return the Promise so caller can't change the Deferred</span></code></div></div><div class="container"><div class="line"><code>    <span class="keyword">return</span> dfd.promise();</code></div></div><div class="container"><div class="line"><code>}</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Attach a done, fail, and progress handler for the asyncEvent</span></code></div></div><div class="container"><div class="line"><code>$.when( asyncEvent() ).then(</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">function</span>(status){</code></div></div><div class="container"><div class="line"><code>        alert( status+<span class="string">', things are going well'</span> );</code></div></div><div class="container"><div class="line"><code>    },</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">function</span>(status){</code></div></div><div class="container"><div class="line"><code>        alert( status+<span class="string">', you fail this time'</span> );</code></div></div><div class="container"><div class="line"><code>    },</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">function</span>(status){</code></div></div><div class="container"><div class="line"><code>        $(<span class="string">"body"</span>).append(status);</code></div></div><div class="container"><div class="line"><code>    }</code></div></div><div class="container"><div class="line"><code>);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">Use the target argument to promote an existing object to a Promise:</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="comment">// Existing object</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> obj = {</code></div></div><div class="container"><div class="line"><code>  hello: <span class="keyword">function</span>( name ) {</code></div></div><div class="container"><div class="line"><code>    alert( <span class="string">"Hello "</span> + name );</code></div></div><div class="container"><div class="line"><code>  }</code></div></div><div class="container"><div class="line"><code>},</code></div></div><div class="container"><div class="line"><code><span class="comment">// Create a Deferred</span></code></div></div><div class="container"><div class="line"><code>defer = $.Deferred();</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Set object as a promise</span></code></div></div><div class="container"><div class="line"><code>defer.promise( obj );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Resolve the deferred</span></code></div></div><div class="container"><div class="line"><code>defer.resolve( <span class="string">"John"</span> );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Use the object as a Promise</span></code></div></div><div class="container"><div class="line"><code>obj.done(<span class="keyword">function</span>( name ) {</code></div></div><div class="container"><div class="line"><code>  obj.hello( name ); <span class="comment">// will alert "Hello John"</span></code></div></div><div class="container"><div class="line"><code>}).hello( <span class="string">"Karl"</span> ); <span class="comment">// will alert "Hello Karl"</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>